﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.UI;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework;
using Cms.Web.Framework.Extensions;
using iLib.Web.UI;
using Page = System.Web.UI.Page;

namespace Cms.Web.UI.Admin
{
    public partial class ContentPreview : Page, IContentPreviewView
    {
        private ContentPreviewPresenter _presenter;

        #region IContentPreviewView Members

        public int LocaleID
        {
            get { return QueryString.GetInt32ID("localeid"); }
        }


        public int MasterTemplateID
        {
            get { return QueryString.GetInt32ID("mastertemplateid"); }
        }


        public int ContentID
        {
            get { return QueryString.GetInt32ID("contentid"); }
        }


        public int VersionID
        {
            get { return QueryString.GetInt32ID("versionid"); }
        }


        public void SetHtml(string html)
        {
            ContentParser.Parse(html, Page.Form, Url.FullPath);
        }


        public void SetStylesheets(IEnumerable<Stylesheet> stylesheets)
        {
            foreach (Stylesheet stylesheet in stylesheets)
            {
                Header.Controls.Add(new LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\"" +
                                                       Url.VirtualPath + "__System/Stylesheets/" +
                                                       stylesheet.ID + ".css?v=" +
                                                       stylesheet.SourceCode.CurrentVersion.Timestamp + "\" />"));
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ContentPreviewPresenter(this);
            _presenter.Initialize();
            Page.Header.Controls.AddAt(0, new LiteralControl("<base href=\"" + Url.FullPath + "\" />"));
            Page.Header.Controls.Add(
                new LiteralControl(
                    "<link type=\"text/css\" rel=\"stylesheet\" href=\"Admin/Css/SitePreview.css?v=3\" />"));
            Page.AddHeaderScriptInclude(ConfigurationManager.AppSettings["jQueryPath"]);
            Page.AddHeaderScript("sitePreviewMultipleJQueryIncludesFix", "var cms$ = $; $.noConflict(true);");
            Page.AddHeaderScriptInclude("Admin/Js/CmsLib.js");
            Page.AddHeaderScriptInclude("Admin/Js/SitePreview.js?v=5");
            Page.AddHeaderScript("sitePreviewInit", "cms.SitePreview.Init();");
        }
    }
}